package com.hellowx.qydcdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hellowx.qydcdev.dao.BusinessDao;
import com.hellowx.qydcdev.dao.GoodsCategoryDao;
import com.hellowx.qydcdev.dao.GoodsDao;
import com.hellowx.qydcdev.dao.StateDao;
import com.hellowx.qydcdev.entity.*;
import com.hellowx.qydcdev.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created on 2022/1/13.
 *
 * @author Hello WX
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsDao goodsDao;
    @Resource
    private GoodsCategoryDao goodsCategoryDao;
    @Resource
    private BusinessDao businessDao;
    @Resource
    private StateDao stateDao;

    @Override
    public PageInfo<GoodsVO> selectGoodsBySearch(Goods goods, int pageNum, int pageSize, HttpServletRequest request) {
        User loginUser = (User) request.getAttribute("loginUser");
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (goods!=null){
            if (goods.getName()!=null && !"".equals(goods.getName().trim())){
                wrapper.like("name",goods.getName().trim());
            }
            if (goods.getCategoryId()!=null){
                wrapper.eq("category_id",goods.getCategoryId());
            }
        }
        if (loginUser!=null){
            if (loginUser.getBusinessId()!=0L){
                wrapper.eq("business_id",loginUser.getBusinessId());
            }
        }
        wrapper.eq("status",0);
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goodsList = goodsDao.selectList(wrapper);
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
        List<GoodsVO> goodsVOList = new ArrayList<>();
        if (goodsList!=null){
            goodsList.forEach(item->{
                GoodsVO goodsVO = new GoodsVO();
                BeanUtils.copyProperties(item,goodsVO);
                GoodsCategory goodsCategory = new LambdaQueryChainWrapper<>(goodsCategoryDao)
                        .eq(GoodsCategory::getId, item.getCategoryId())
                        .one();
                goodsVO.setGoodsCategoryName(goodsCategory.getName());
                Business business = new LambdaQueryChainWrapper<>(businessDao)
                        .eq(Business::getId, item.getBusinessId())
                        .one();
                goodsVO.setBusinessName(business.getName());
                goodsVOList.add(goodsVO);
            });
        }
        PageInfo<GoodsVO> goodsVOPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(goodsPageInfo,goodsVOPageInfo);
        goodsVOPageInfo.setList(goodsVOList);
        return goodsVOPageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteGoodsById(Long goodsId) {
        Goods goods = new Goods();
        goods.setId(goodsId);
        goods.setStatus(1);
        int rows = goodsDao.updateById(goods);
        return rows>0? "删除成功!" : "删除失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertGoods(Goods goods) {
        int rows =goodsDao.insert(goods);
        return goods.getId()+"";
    }

    @Override
    public List<State> listStateListByGoodsId(Long goodsId) {
        return new LambdaQueryChainWrapper<>(stateDao)
                .eq(State::getGoodsId,goodsId)
                .list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addState(State state) {
        return stateDao.insert(state);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteState(Long id) {
        return stateDao.deleteById(id);
    }

    @Override
    public GoodsVO getGoodsById(Long goodsId) {
        Goods goods = new LambdaQueryChainWrapper<>(goodsDao)
                .eq(Goods::getId, goodsId)
                .one();
        GoodsVO goodsVO = new GoodsVO();
        if (goods!=null){
            BeanUtils.copyProperties(goods,goodsVO);
        }
        return goodsVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateGoodsById(Goods goods) {
        int rows = goodsDao.updateById(goods);
        return rows>0 ? "修改成功!" : "修改失败!";
    }
}
